import configparser


class configReaderUtils:
    DETECT_TYPE_HAND = 'hand'
    DETECT_TYPE_ORAL = 'oralCavity'
    DETECT_TYPE_FACE = 'face'
    # 定义全局变量
    hostAddr, port, imgPrefix, staticResource = None, None, None, None
    handHurtDetectModelPath, oralHurtDetectModelPath, faceHurtDetectModelPath = None, None, None
    imgSavaPathDict = {}
    imgResultPathDict = {}
    detectModelDict = {}
    imgPrxfixDict={}
    deviceDict = {}
    halfDict = {}

    def __init__(self):
        print("--")

    # 读取配置文件
    @classmethod
    def readConfig(cls):
        # 实例化ConfigParser
        cf = configparser.ConfigParser()
        cf.read('config.ini')

        cls.handHurtDetectModelPath = cf.get('dataDir', 'handHurtDetectModelPath')
        handHurtDetectImageSaveDir = cf.get('dataDir', 'handHurtDetectImageSaveDir')
        handHurtDetectImageResultPath = cf.get('dataDir', 'handHurtDetectImageResultPath')

        cls.oralHurtDetectModelPath = cf.get('dataDir', 'oralHurtDetectModelPath')
        oralHurtDetectImageSaveDir = cf.get('dataDir', 'oralHurtDetectImageSaveDir')
        oralHurtDetectImageResultPath = cf.get('dataDir', 'oralHurtDetectImageResultPath')

        cls.faceHurtDetectModelPath = cf.get('dataDir', 'faceHurtDetectModelPath')
        faceHurtDetectImageSaveDir = cf.get('dataDir', 'faceHurtDetectImageSaveDir')
        faceHurtDetectImageResultPath = cf.get('dataDir', 'faceHurtDetectImageResultPath')

        cls.imgSavaPathDict[cls.DETECT_TYPE_HAND] = handHurtDetectImageSaveDir
        cls.imgResultPathDict[cls.DETECT_TYPE_HAND] = handHurtDetectImageResultPath

        cls.imgSavaPathDict[cls.DETECT_TYPE_ORAL] = oralHurtDetectImageSaveDir
        cls.imgResultPathDict[cls.DETECT_TYPE_ORAL] = oralHurtDetectImageResultPath

        cls.imgSavaPathDict[cls.DETECT_TYPE_FACE] = faceHurtDetectImageSaveDir
        cls.imgResultPathDict[cls.DETECT_TYPE_FACE] = faceHurtDetectImageResultPath

        cls.staticResource = cf.get('dataDir', 'staticResource')

        cls.hostAddr = cf.get('service', 'hostAddr')
        cls.port = cf.get('service', 'port')
        cls.imgPrefix = cf.get('service', 'imgPrefixBase')

        cls.imgPrxfixDict[cls.DETECT_TYPE_HAND]=cls.imgPrefix+cf.get('dataDir', 'handImgPrefix')
        cls.imgPrxfixDict[cls.DETECT_TYPE_ORAL]=cls.imgPrefix + cf.get('dataDir', 'oralImgPrefix')
        cls.imgPrxfixDict[cls.DETECT_TYPE_FACE]=cls.imgPrefix + cf.get('dataDir', 'faceImgPrefix')

        print("-------------------read config parameters[start]-------------------\r\n")
        print("handHurtDetectModelPath:" + cls.handHurtDetectModelPath + "\r\n")
        print("oralHurtDetectModelPath:" + cls.oralHurtDetectModelPath + "\r\n")
        print("handHurtDetectModelPath:" + cls.faceHurtDetectModelPath + "\r\n")
        print("imgSavaPathDict:" + str(cls.imgSavaPathDict) + "\r\n")
        print("imgResultPathDict:" + str(cls.imgResultPathDict) + "\r\n")
        print("hostAddr:" + cls.hostAddr + "\r\n")
        print("port:" + cls.port + "\r\n")
        print("-------------------read config parameters[end]-------------------\r\n")

    @classmethod
    def loadDetectModel(cls, yos):
        # 加载检测模型
        print("*******************loadDetectModel[start]*******************")

        handModel, handDevice, handHalf = yos.loadModel(cls.handHurtDetectModelPath, 'cpu')

        oralCavityModel, oralDevice, oralHalf = yos.loadModel(cls.oralHurtDetectModelPath, 'cpu')

        faceHurtDetectModelPath, faceDevice, faceHalf = yos.loadModel(cls.faceHurtDetectModelPath, 'cpu')

        cls.detectModelDict[cls.DETECT_TYPE_HAND] = handModel
        cls.detectModelDict[cls.DETECT_TYPE_ORAL] = oralCavityModel
        cls.detectModelDict[cls.DETECT_TYPE_FACE] = faceHurtDetectModelPath

        cls.deviceDict[cls.DETECT_TYPE_HAND] = handDevice
        cls.deviceDict[cls.DETECT_TYPE_ORAL] = oralDevice
        cls.deviceDict[cls.DETECT_TYPE_FACE] = faceDevice

        cls.halfDict[cls.DETECT_TYPE_HAND] = handHalf
        cls.halfDict[cls.DETECT_TYPE_ORAL] = oralHalf
        cls.halfDict[cls.DETECT_TYPE_FACE] = faceHalf

        print("*******************loadDetectModel[end]*******************")


    @staticmethod
    def getDetctResrouce(detectType):
        return configReaderUtils.detectModelDict[detectType], configReaderUtils.deviceDict[detectType], configReaderUtils.halfDict[detectType], \
               configReaderUtils.imgSavaPathDict[detectType], \
               configReaderUtils.imgResultPathDict[detectType],\
               configReaderUtils.imgPrxfixDict[detectType]